﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using ACIT.Util.PowerReflection;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraLayout;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraTreeList;
using DevExpress.XtraGrid.Columns;
using DevExpress.Utils;


namespace UIShare
{
    public static class ControlHelper
    {
        public static void InitControls(Control control)
        {
            #region Init BarManager

            IList<BarManager> barManagers = GetControl<BarManager>(control);

            foreach (var item in barManagers)
            {
                item.AllowShowToolbarsPopup = false;
                item.AllowQuickCustomization = false;
                item.AllowMoveBarOnToolbar = false;
                item.AllowCustomization = false;
            }

            #endregion

            #region Init Bar

            IList<Bar> bars = GetControl<Bar>(control);

            foreach (var item in bars)
            {
                item.OptionsBar.DrawDragBorder = false;
            }

            #endregion

            #region Init LayoutControl

            IList<LayoutControl> layoutControls = GetControl<LayoutControl>(control);

            foreach (var item in layoutControls)
            {
                item.AllowCustomizationMenu = false;
            }

            #endregion

            #region Init GridView

            IList<GridView> gridViews = GetControl<GridView>(control);

            foreach (var item in gridViews)
            {
                item.OptionsMenu.EnableColumnMenu = false;
                item.OptionsMenu.EnableFooterMenu = false;
                item.OptionsMenu.EnableGroupPanelMenu = false;
                item.OptionsMenu.ShowDateTimeGroupIntervalItems = false;
                item.OptionsMenu.ShowGroupSortSummaryItems = false;
                item.OptionsMenu.ShowGroupSummaryEditorItem = false;

                item.OptionsView.ColumnAutoWidth = false;
                item.OptionsView.EnableAppearanceEvenRow = true;
                item.OptionsView.ShowGroupPanel = false;

                item.OptionsFilter.AllowColumnMRUFilterList = false;

                item.OptionsSelection.MultiSelect = false;

                item.OptionsDetail.EnableMasterViewMode = false;
            }
            #endregion

            #region Init GridColumn
            IList<GridColumn> gridColumns = GetControl<GridColumn>(control, false);

            foreach (var item in gridColumns)
            {
                item.OptionsFilter.AutoFilterCondition = AutoFilterCondition.Contains;
            }
            #endregion

            #region Init PanelControl

            IList<PanelControl> panelControls = GetControl<PanelControl>(control, false);

            foreach (var item in panelControls)
            {
                item.BorderStyle = BorderStyles.NoBorder;
            }

            #endregion

            #region Init TreeList
            IList<TreeList> treeLists = GetControl<TreeList>(control, true);

            foreach (var item in treeLists)
            {
                item.OptionsMenu.EnableColumnMenu = false;
                item.OptionsMenu.EnableFooterMenu = false;

                item.OptionsView.ShowIndicator = false;
                item.OptionsView.ShowColumns = false;
                item.OptionsView.ShowVertLines = false;
                item.OptionsView.ShowHorzLines = false;

                item.OptionsBehavior.Editable = false;
            }
            #endregion

            #region Init SpinEdit
            IList<SpinEdit> spinEdits = GetControl<SpinEdit>(control, false);

            foreach (var item in spinEdits)
            {
                item.Properties.MinValue = 0;

                item.Properties.DisplayFormat.FormatType = FormatType.Numeric;
                if (item.Properties.IsFloatValue)
                    item.Properties.DisplayFormat.FormatString = "#,0.##";
                else
                    item.Properties.DisplayFormat.FormatString = "#,#";

                item.Properties.Click += (s, e) => { ((SpinEdit)s).SelectAll(); };
            }
            #endregion

            #region Init GridLookupEdit
            IList<GridLookUpEdit> gridLookupEdits = GetControl<GridLookUpEdit>(control, false);

            foreach (var item in gridLookupEdits)
            {
                item.Properties.PopupSizeable = true;

                item.Properties.AutoComplete = false;
                item.Properties.PopupFilterMode = PopupFilterMode.Contains;
                item.Properties.PopupFormSize = new System.Drawing.Size(0, 800);
            }
            #endregion
        }

        public static IList<T> GetControl<T>(object container)
        {
            return GetControl<T>(container, true);
        }

        public static IList<T> GetControl<T>(object container, bool isInherit)
        {
            List<T> resultList = new List<T>();

            FieldInfo[] fieldInfoList = ReflectionUtilities.GetAllFieldInfo(container.GetType());

            foreach (FieldInfo fieldInfo in fieldInfoList)
            {
                object control = fieldInfo.GetValue(container);

                if (isInherit)
                {
                    if (!(control is T))
                        continue;
                }
                else
                {
                    if (control == null || control.GetType().FullName != typeof(T).FullName)
                        continue;
                }

                T t = (T)control;
                if (resultList.Contains(t))
                    continue;

                resultList.Add(t);
            }

            return resultList;
        }
    }
}
